/**
 * Created by ChenYong on 2017-05-24.
 *
 * 举报统计。
 */

$(function () {
    /** 年份的 jQuery 对象 */
    var $year = $("#year");
    /** 月份的 jQuery 对象 */
    var $month = $("#month");
    /** 搜索按钮的 jQuery 对象 */
    var $searchBtn = $("#search_btn");
    /** 举报统计图表的 jQuery 对象 */
    var $reportCountChart = $("#report_count_chart");
    /** 举报统计发红包图表的 jQuery 对象 */
    var $reportCountSendredpackChart = $("#report_count_sendredpack_chart");
    /** 举报统计领红包图表的 jQuery 对象 */
    var $reportCountReceiveredpackChart = $("#report_count_receiveredpack_chart");
    /** 举报统计图表 */
    var reportCountChart = echarts.init($reportCountChart.get(0));
    /** 举报统计发红包图表 */
    var reportCountSendredpackChart = echarts.init($reportCountSendredpackChart.get(0));
    /** 举报统计领红包图表 */
    var reportCountReceiveredpackChart = echarts.init($reportCountReceiveredpackChart.get(0));
    /** 举报统计图表的配置 */
    var reportCountChartOption = {
        title: {
            x: "center",
            text: "",
            textStyle: {
                fontSize: 20
            }
        },
        tooltip: {
            trigger: "item",
            formatter: "{a}<br/>{b}：{c}次（{d}%）"
        },
        legend: {
            data: [],
            left: "center",
            bottom: "bottom"
        },
        series: [{
            name: "举报来源",
            data: [],
            type: "pie",
            radius: "70%",
            center: ["50%", "50%"],
            label: {
                normal: {
                    show: true,
                    position: "outside",
                    formatter: "{b}（{c}次）"
                }
            }
        }]
    };
    /** 举报统计发红包图表的配置 */
    var reportCountSendredpackChartOption = {
        title: {
            x: "center",
            text: "",
            textStyle: {
                fontSize: 20
            }
        },
        tooltip: {
            trigger: "item",
            formatter: "{a}<br/>{b}：{c}个（{d}%）"
        },
        legend: {
            data: [],
            left: "center",
            bottom: "bottom"
        },
        series: [{
            name: "发放红包",
            data: [],
            type: "pie",
            radius: "70%",
            center: ["50%", "50%"],
            label: {
                normal: {
                    show: true,
                    position: "outside",
                    formatter: "{b}（{c}个）"
                }
            }
        }]
    };
    /** 举报统计领红包图表的配置 */
    var reportCountReceiveredpackChartOption = {
        title: {
            x: "center",
            text: "",
            textStyle: {
                fontSize: 20
            }
        },
        tooltip: {
            trigger: "item",
            formatter: "{a}<br/>{b}：{c}个（{d}%）"
        },
        legend: {
            data: [],
            left: "center",
            bottom: "bottom"
        },
        series: [{
            name: "领取红包",
            data: [],
            type: "pie",
            radius: "70%",
            center: ["50%", "50%"],
            label: {
                normal: {
                    show: true,
                    position: "outside",
                    formatter: function (params, ticket, callback) {
                        var tip = "{0}（{1}个，总金额：{2}元）".format(params.name, params.value, params.data.money)

                        return tip;
                    }
                }
            }
        }]
    };

    // 点击搜索
    $searchBtn.click(function () {
        getReportCount();
    });

    getReportCount();

    /**
     *
     * 获取举报统计。
     *
     */
    function getReportCount() {
        $.myAjax({
            url: "web/report/count.json",
            submitBtn: $searchBtn,
            submitMsg: "正在搜索",
            data: {
                month: ($year.val() + "-" + $month.val())
            },
            successFn: function (data) {
                var data1 = new Array();
                data1.push(new Array());
                data1.push(new Array());

                $.each(data.results[1], function (i, e) {
                    data1[0].push(e.reportSource);
                    data1[1].push({
                        name: e.reportSource,
                        value: e.reportCount
                    });
                });

                reportCountChartOption.title.text = "{0}举报统计".format(data.results[0]);
                reportCountChartOption.legend.data = data1[0];
                reportCountChartOption.series[0].data = data1[1];

                reportCountChart.setOption(reportCountChartOption);
            }
        });

        $.myAjax({
            url: "web/report/sendredpack/count.json",
            submitBtn: $searchBtn,
            submitMsg: "正在搜索",
            data: {
                month: ($year.val() + "-" + $month.val())
            },
            successFn: function (data) {
                var data1 = new Array();
                data1.push(new Array());
                data1.push(new Array());

                $.each(data.results[1], function (i, e) {
                    data1[0].push(e.sendredpackResult);
                    data1[1].push({
                        name: e.sendredpackResult,
                        value: e.sendredpackCount
                    });
                });

                reportCountSendredpackChartOption.title.text = "{0}发放红包统计".format(data.results[0]);
                reportCountSendredpackChartOption.legend.data = data1[0];
                reportCountSendredpackChartOption.series[0].data = data1[1];

                reportCountSendredpackChart.setOption(reportCountSendredpackChartOption);
            }
        });

        $.myAjax({
            url: "web/report/receiveredpack/count.json",
            submitBtn: $searchBtn,
            submitMsg: "正在搜索",
            data: {
                month: ($year.val() + "-" + $month.val())
            },
            successFn: function (data) {
                var data1 = new Array();
                data1.push(new Array());
                data1.push(new Array());

                $.each(data.results[1], function (i, e) {
                    data1[0].push(e.receiveredpackResult);
                    data1[1].push({
                        name: e.receiveredpackResult,
                        value: e.receiveredpackCount,
                        money: e.receiveredpackMoney
                    });
                });

                reportCountReceiveredpackChartOption.title.text = "{0}领取红包统计".format(data.results[0]);
                reportCountReceiveredpackChartOption.legend.data = data1[0];
                reportCountReceiveredpackChartOption.series[0].data = data1[1];

                reportCountReceiveredpackChart.setOption(reportCountReceiveredpackChartOption);
            }
        });
    }
});